package com.woniuxy.cloud.carl.car.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cloud.Aspect.log.Log;
import com.woniuxy.cloud.carl.car.client.CarQueryClient;
import com.woniuxy.cloud.carl.car.client.param.GetCarByConditionParam;
import com.woniuxy.cloud.carl.car.client.param.GetCarByYearParam;
import com.woniuxy.cloud.carl.car.client.param.GetStyleParam;
import com.woniuxy.cloud.carl.car.client.result.CarForStationDto;
import com.woniuxy.cloud.carl.car.client.result.WoniuPageDto;
import com.woniuxy.cloud.carl.car.infra.mapper.CarMapper;
import com.woniuxy.cloud.carl.car.infra.model.*;
import com.woniuxy.cloud.carl.car.infra.service.*;
import com.woniuxy.result.Result;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class CarQueryApiImpl implements CarQueryClient {

    @Resource
    private CarBrandService brandService;
    @Resource
    private CarSortService sortService;
    @Resource
    private CarRepositoryService styleService;
    @Resource
    private ProductYearService yearService;
    @Resource
    private CarService carService;
    @Resource
    private CarMapper mapper;

    @Override
    @Log(value = "条件查询车辆")
    public Result getCars(GetCarByConditionParam param) {
        Page<Car> page = new Page<>( param.getPageSize() == null ? 1 : param.getPageSize(),param.getPageNum() == null ? 3 : param.getPageNum());
        QueryWrapper<Car> wrapper = new QueryWrapper<Car>();
        if(param.getCarBrandId()!=null) wrapper.eq("car_brand_id",param.getCarBrandId());
        if(param.getCarSortId()!=null) wrapper.eq("car_sort_id",param.getCarSortId());
        if(param.getCarStyleId()!=null) wrapper.eq("car_style_id",param.getCarStyleId());
        if(param.getProductYearId()!=null) wrapper.eq("product_year_id",param.getProductYearId());
        if(param.getDisplacement()!=null) wrapper.eq("displacement",param.getDisplacement());
        mapper.selectPage(page, wrapper);

        ArrayList<CarForStationDto> dtos = new ArrayList<>();
        for (Car car : page.getRecords()) {
            CarForStationDto dto = BeanUtil.toBean(car, CarForStationDto.class);
            dtos.add(dto);
        }
        WoniuPageDto woniuPageDto = BeanUtil.toBean(page, WoniuPageDto.class);
        woniuPageDto.setList(dtos);
        return Result.success(woniuPageDto);
    }

    @Override
    @Log(value = "通过品牌id获得所有车系")
    public Result getSortsByBrandId(Long carBrandId) {
        List<CarSort> carSorts = sortService.list(new QueryWrapper<CarSort>().eq("car_brand_id", carBrandId));
        return Result.success(carSorts);
    }

    @Override
    @Log(value = "通过车系获得车型")
    public Result getStyleBySort(GetStyleParam param) {
        QueryWrapper<CarRepository> wrapper = new QueryWrapper<>();
        if(param.getCarSortId()!=null) wrapper.eq("sort_id",param.getCarSortId());
        if(param.getDisplacement()!=null) wrapper.eq("displacement",param.getDisplacement());
        List<CarRepository> list = styleService.list(wrapper);
        return Result.success(list);
    }

    @Override
    @Log(value = "通过年份车型选择车辆")
    public Result getCarByStyleId(GetCarByYearParam param) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("product_year_id",param.getProductYearId()).eq("car_style_id",param.getCarStyleId());
        List<Car> list = carService.list(wrapper);
        return Result.success(list);
    }

    @Override
    @Log(value = "获得所有品牌")
    public Result getBrands() {
        List<CarBrand> list = brandService.list();
        return Result.success(list);
    }

    @Override
    @Log(value = "通过汽车id获得详情")
    public Result<CarForStationDto> getDetail(Long carId) {
        Car car = carService.getById(carId);
        CarForStationDto dto = BeanUtil.toBean(car, CarForStationDto.class);
        return Result.success(dto);
    }

    @Override
    @Log(value = "通过车型id获得生产年份")
    public Result getYearByStyleId(Long carStyleId) {
        QueryWrapper<Car> wrapper = new QueryWrapper<Car>().eq("car_style_id", carStyleId);
        List<Car> cars = carService.list(wrapper);
        ArrayList<ProductYear> years = new ArrayList<>();
        for (Car car : cars) {
            ProductYear year = yearService.getById(car.getProductYearId());
            years.add(year);
        }
        return Result.success(years);
    }

    @Override
    @Log(value = "通过车系id获得排量")
    public Result getDisplacementBySortId(Long carSortId) {
        QueryWrapper<CarRepository> wrapper = new QueryWrapper<CarRepository>().eq("sort_id", carSortId);
        List<CarRepository> list = styleService.list(wrapper);
        ArrayList<String> displacements = new ArrayList<>();
        for (CarRepository style : list) {
            if(!displacements.contains(style.getDisplacement())){
                displacements.add(style.getDisplacement());
            }

        }
        return Result.success(displacements);
    }
}
